const express = require('express');
const router = express.Router();
const db = require('../../../util/dbConfig/dbConfig')
const nowDate = require('../../../public/javascripts/nowDate')


router.post('/',async (req,res) => {
    let userInfo = req.session.userInfo
    let {
        diameterMax,   //直径最大值
        diameterMin,    //直径最小值
        diameter_differenceMax, //直径标准差最大值
        diameter_differenceMin, //直径标准差最小值
        lengthMax,     //长度最大值
        lengthMin,      //长度最小值
        length_lengthMax,  //长度标准差最大值
        length_lengthMin,  //长度标准差最小值
        length_codefficientMax,     //长度变异系数最大值
        length_codefficientMin,       //长度变异系数最小值
        year,                          //年度
    } = req.body
    if(!year){
        //默认当前年份
        year = nowDate().year + ''
    }
    year = year + ''
    if(year.length !== 4){
        return res.send({
            code: 1,
            msg: '年份格式错误'
        })
    }
    let dateArr = [
        [
            diameterMax,   //直径最大值
            diameterMin,    //直径最小值
        ],
        [
            diameter_differenceMax, //直径标准差最大值
            diameter_differenceMin, //直径标准差最小值
        ],
        [
            lengthMax,     //长度最大值
            lengthMin,      //长度最小值
        ],
        [
            length_lengthMax,  //长度标准差最大值
            length_lengthMin,  //长度标准差最小值
        ],
        [
            length_codefficientMax,     //长度变异系数最大值
            length_codefficientMin,       //长度变异系数最小值
        ],

    ]
    let nameArr = [
        'DIAMETER',
        'D_DIFFERENCE',
        'LENGTH',
        'L_DIFFERENCE',
        'L_CODEFFICIENT'
    ]
    let dataArrA = []
    let nameArrA = []

    for(let i =0; i< dateArr.length ; i++){
        for(let j =0; j<dateArr[i].length;j++){
            if(dateArr[i][j] !== null && dateArr[i][j] !== ''  && dateArr[i][j+1] !== null && dateArr[i][j+1] !== ''){
                dataArrA.push(dateArr[i][j])
                dataArrA.push(dateArr[i][j+1])
                nameArrA.push(nameArr[i])
                j++
            }
        }
    }
    if(nameArr.length !== 1 && dataArrA.length !== 2){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    try{
        let sql = `select ${nameArrA[0]} from sheepftne
        where ${nameArrA[0]}<=${dataArrA[0]} and 
        ${nameArrA[0]}>=${dataArrA[1]} and 
        YEAR(MEASUREMENT_DATE)=${year}
        `
        //该用户是普通用户
        if(userInfo.admin === 0){
            sql = `select a.${nameArrA[0]} from sheepftne a,sheep b
        where a.SHEEP_ID=b.ID and 
        b.USER_ID= "${userInfo._id}" and
        ${nameArrA[0]}<=${dataArrA[0]} and 
        ${nameArrA[0]}>=${dataArrA[1]} and 
        YEAR(MEASUREMENT_DATE)=${year}
        `
        }
        sql = sql.replace("'"," ")

        let result = await db.SySqlConnect(sql,[])
        if(result.length === 0){
            return res.send({
                code: 2,
                msg: '无数据'
            })
        }
        res.send({
            code: 0,
            msg: `查询到有${result.length}条数据`,
            data: {
                length: result.length, //共有多少条数据
                result
            }
        })
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }

})

module.exports = router


